
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>页面置换* · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    
    <link rel="stylesheet" href="../../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-chapter-fold/chapter-fold.css">
                
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-katex/katex.min.css">
                
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-alerts/style.css">
                
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-emphasize/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-mermaid-gb3/mermaid/mermaid.css">
                
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-codeblock-label/block.css">
                
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-code/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-search-pro/search.css">
                
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-click-reveal/click_reveal.css">
                
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-expandable-chapters-interactive/expandable-chapters.css">
                
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    


    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="summary.html" />
    
    
    <link rel="prev" href="part-4.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../../">
            
                <a href="../../../">
            
                    
                    实验简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../../../notes/log.html">
            
                <a href="../../../notes/log.html">
            
                    
                    更新日志
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">实验之前</li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="../../pre-lab/env.html">
            
                <a href="../../pre-lab/env.html">
            
                    
                    环境部署
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="../../pre-lab/gdb.html">
            
                <a href="../../pre-lab/gdb.html">
            
                    
                    GDB 调试方法*
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">实验指导</li>
        
        
    
        <li class="chapter " data-level="3.1" >
            
                <span>
            
                    
                    实验指导零
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1.1" data-path="../../lab-0/guide/intro.html">
            
                <a href="../../lab-0/guide/intro.html">
            
                    
                    摘要
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.2" data-path="../../lab-0/guide/part-1.html">
            
                <a href="../../lab-0/guide/part-1.html">
            
                    
                    创建项目
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.3" data-path="../../lab-0/guide/part-2.html">
            
                <a href="../../lab-0/guide/part-2.html">
            
                    
                    移除标准库依赖
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.4" data-path="../../lab-0/guide/part-3.html">
            
                <a href="../../lab-0/guide/part-3.html">
            
                    
                    移除运行时环境依赖
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.5" data-path="../../lab-0/guide/part-4.html">
            
                <a href="../../lab-0/guide/part-4.html">
            
                    
                    编译为裸机目标
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.6" data-path="../../lab-0/guide/part-5.html">
            
                <a href="../../lab-0/guide/part-5.html">
            
                    
                    生成内核镜像
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.7" data-path="../../lab-0/guide/part-6.html">
            
                <a href="../../lab-0/guide/part-6.html">
            
                    
                    调整内存布局
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.8" data-path="../../lab-0/guide/part-7.html">
            
                <a href="../../lab-0/guide/part-7.html">
            
                    
                    重写程序入口点
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.9" data-path="../../lab-0/guide/part-8.html">
            
                <a href="../../lab-0/guide/part-8.html">
            
                    
                    使用 QEMU 运行
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.10" data-path="../../lab-0/guide/part-9.html">
            
                <a href="../../lab-0/guide/part-9.html">
            
                    
                    接口封装和代码整理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.11" data-path="../../lab-0/guide/summary.html">
            
                <a href="../../lab-0/guide/summary.html">
            
                    
                    小结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.2" >
            
                <span>
            
                    
                    实验指导一
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.2.1" data-path="../../lab-1/guide/intro.html">
            
                <a href="../../lab-1/guide/intro.html">
            
                    
                    摘要
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2.2" data-path="../../lab-1/guide/part-1.html">
            
                <a href="../../lab-1/guide/part-1.html">
            
                    
                    什么是中断
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2.3" data-path="../../lab-1/guide/part-2.html">
            
                <a href="../../lab-1/guide/part-2.html">
            
                    
                    RISC-V 中的中断
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2.4" data-path="../../lab-1/guide/part-3.html">
            
                <a href="../../lab-1/guide/part-3.html">
            
                    
                    程序运行状态
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2.5" data-path="../../lab-1/guide/part-4.html">
            
                <a href="../../lab-1/guide/part-4.html">
            
                    
                    状态的保存与恢复
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2.6" data-path="../../lab-1/guide/part-5.html">
            
                <a href="../../lab-1/guide/part-5.html">
            
                    
                    进入中断处理流程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2.7" data-path="../../lab-1/guide/part-6.html">
            
                <a href="../../lab-1/guide/part-6.html">
            
                    
                    时钟中断
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2.8" data-path="../../lab-1/guide/summary.html">
            
                <a href="../../lab-1/guide/summary.html">
            
                    
                    小结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.3" >
            
                <span>
            
                    
                    实验指导二
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.3.1" data-path="../../lab-2/guide/intro.html">
            
                <a href="../../lab-2/guide/intro.html">
            
                    
                    摘要
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3.2" data-path="../../lab-2/guide/part-1.html">
            
                <a href="../../lab-2/guide/part-1.html">
            
                    
                    动态内存分配
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3.3" data-path="../../lab-2/guide/part-2.html">
            
                <a href="../../lab-2/guide/part-2.html">
            
                    
                    物理内存探测
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3.4" data-path="../../lab-2/guide/part-3.html">
            
                <a href="../../lab-2/guide/part-3.html">
            
                    
                    物理内存管理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3.5" data-path="../../lab-2/guide/summary.html">
            
                <a href="../../lab-2/guide/summary.html">
            
                    
                    小结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.4" >
            
                <span>
            
                    
                    实验指导三
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.4.1" data-path="intro.html">
            
                <a href="intro.html">
            
                    
                    摘要
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4.2" data-path="part-1.html">
            
                <a href="part-1.html">
            
                    
                    从虚拟地址到物理地址
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4.3" data-path="part-2.html">
            
                <a href="part-2.html">
            
                    
                    修改内核
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4.4" data-path="part-3.html">
            
                <a href="part-3.html">
            
                    
                    实现页表
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4.5" data-path="part-4.html">
            
                <a href="part-4.html">
            
                    
                    实现内核重映射
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="3.4.6" data-path="part-5.html">
            
                <a href="part-5.html">
            
                    
                    页面置换*
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4.7" data-path="summary.html">
            
                <a href="summary.html">
            
                    
                    小结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.5" >
            
                <span>
            
                    
                    实验指导四
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.5.1" data-path="../../lab-4/guide/intro.html">
            
                <a href="../../lab-4/guide/intro.html">
            
                    
                    摘要
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.5.2" data-path="../../lab-4/guide/part-1.html">
            
                <a href="../../lab-4/guide/part-1.html">
            
                    
                    线程和进程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.5.3" data-path="../../lab-4/guide/part-2.html">
            
                <a href="../../lab-4/guide/part-2.html">
            
                    
                    线程的创建
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.5.4" data-path="../../lab-4/guide/part-3.html">
            
                <a href="../../lab-4/guide/part-3.html">
            
                    
                    线程的切换
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.5.5" data-path="../../lab-4/guide/part-4.html">
            
                <a href="../../lab-4/guide/part-4.html">
            
                    
                    线程的结束
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.5.6" data-path="../../lab-4/guide/part-5.html">
            
                <a href="../../lab-4/guide/part-5.html">
            
                    
                    内核栈
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.5.7" data-path="../../lab-4/guide/part-6.html">
            
                <a href="../../lab-4/guide/part-6.html">
            
                    
                    线程调度
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.5.8" data-path="../../lab-4/guide/summary.html">
            
                <a href="../../lab-4/guide/summary.html">
            
                    
                    小结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.6" >
            
                <span>
            
                    
                    实验指导五
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.6.1" data-path="../../lab-5/guide/intro.html">
            
                <a href="../../lab-5/guide/intro.html">
            
                    
                    摘要
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.6.2" data-path="../../lab-5/guide/part-1.html">
            
                <a href="../../lab-5/guide/part-1.html">
            
                    
                    设备树
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.6.3" data-path="../../lab-5/guide/part-2.html">
            
                <a href="../../lab-5/guide/part-2.html">
            
                    
                    virtio
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.6.4" data-path="../../lab-5/guide/part-3.html">
            
                <a href="../../lab-5/guide/part-3.html">
            
                    
                    驱动和块设备驱动
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.6.5" data-path="../../lab-5/guide/part-4.html">
            
                <a href="../../lab-5/guide/part-4.html">
            
                    
                    文件系统
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.6.6" data-path="../../lab-5/guide/summary.html">
            
                <a href="../../lab-5/guide/summary.html">
            
                    
                    小结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.7" >
            
                <span>
            
                    
                    实验指导六
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.7.1" data-path="../../lab-6/guide/intro.html">
            
                <a href="../../lab-6/guide/intro.html">
            
                    
                    摘要
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.7.2" data-path="../../lab-6/guide/part-1.html">
            
                <a href="../../lab-6/guide/part-1.html">
            
                    
                    构建用户程序框架
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.7.3" data-path="../../lab-6/guide/part-2.html">
            
                <a href="../../lab-6/guide/part-2.html">
            
                    
                    打包为磁盘镜像
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.7.4" data-path="../../lab-6/guide/part-3.html">
            
                <a href="../../lab-6/guide/part-3.html">
            
                    
                    解析 ELF 文件并创建线程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.7.5" data-path="../../lab-6/guide/part-4.html">
            
                <a href="../../lab-6/guide/part-4.html">
            
                    
                    实现系统调用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.7.6" data-path="../../lab-6/guide/part-5.html">
            
                <a href="../../lab-6/guide/part-5.html">
            
                    
                    处理文件描述符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.7.7" data-path="../../lab-6/guide/part-6.html">
            
                <a href="../../lab-6/guide/part-6.html">
            
                    
                    条件变量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.7.8" data-path="../../lab-6/guide/summary.html">
            
                <a href="../../lab-6/guide/summary.html">
            
                    
                    小结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="header">实验题</li>
        
        
    
        <li class="chapter " data-level="4.1" data-path="../../lab-1/practice.html">
            
                <a href="../../lab-1/practice.html">
            
                    
                    实验一：中断
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="../../lab-2/practice.html">
            
                <a href="../../lab-2/practice.html">
            
                    
                    实验二：内存分配
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="../practice.html">
            
                <a href="../practice.html">
            
                    
                    实验三：虚实地址转换
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="../../lab-4/practice-1.html">
            
                <a href="../../lab-4/practice-1.html">
            
                    
                    实验四（上）：线程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="../../lab-4/practice-2.html">
            
                <a href="../../lab-4/practice-2.html">
            
                    
                    实验四（下）：线程调度
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.6" data-path="../../lab-6/practice.html">
            
                <a href="../../lab-6/practice.html">
            
                    
                    实验六：系统调用
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">开发笔记</li>
        
        
    
        <li class="chapter " data-level="5.1" data-path="../../format/partition.html">
            
                <a href="../../format/partition.html">
            
                    
                    文档代码划分
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="../../format/doc.html">
            
                <a href="../../format/doc.html">
            
                    
                    文档格式规范
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="../../format/code.html">
            
                <a href="../../format/code.html">
            
                    
                    代码格式规范
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../../.." >页面置换*</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h2 id="&#x9875;&#x9762;&#x7F6E;&#x6362;">&#x9875;&#x9762;&#x7F6E;&#x6362;*</h2>
<blockquote>
<p><strong>[info] &#x6CE8;&#x610F;</strong>
&#x672C;&#x5C0F;&#x8282;&#x6D89;&#x53CA;&#x5185;&#x5BB9;&#x7E41;&#x6742;&#xFF0C;&#x5B9E;&#x73B0;&#x4E5F;&#x53EF;&#x80FD;&#x6709;&#x8003;&#x8651;&#x4E0D;&#x5468;&#x4E4B;&#x5904;&#xFF0C;&#x5177;&#x4F53;&#x7684;&#x4EE3;&#x7801;&#x4EC5;&#x4F9B;&#x6709;&#x5174;&#x8DA3;&#x7684;&#x540C;&#x5B66;&#x9605;&#x8BFB;&#x3002;</p>
<p>&#x7531;&#x4E8E;&#x9700;&#x8981;&#x64CD;&#x4F5C;&#x201C;&#x9875;&#x9762;&#x7F6E;&#x6362;&#x6587;&#x4EF6;&#x201D;&#xFF0C;&#x9875;&#x9762;&#x7F6E;&#x6362;&#x7684;&#x5177;&#x4F53;&#x5B9E;&#x73B0;&#x4F1A;&#x7528;&#x5230;&#x6587;&#x4EF6;&#x7CFB;&#x7EDF;&#xFF08;&#x5B9E;&#x9A8C;&#x4E94;&#xFF09;&#x7684;&#x6846;&#x67B6;&#x3002;&#x4E0D;&#x8FC7;&#xFF0C;&#x5B9E;&#x9A8C;&#x4E94;&#x4F1A;&#x63D0;&#x4F9B;&#x975E;&#x5E38;&#x62BD;&#x8C61;&#x7684;&#x63A5;&#x53E3;&#xFF0C;&#x6211;&#x4EEC;&#x6682;&#x65F6;&#x4E0D;&#x9700;&#x8981;&#x5B8C;&#x5168;&#x4E86;&#x89E3;&#x540E;&#x9762;&#x5B9E;&#x9A8C;&#x7684;&#x5B9E;&#x73B0;&#x65B9;&#x6CD5;&#x3002;</p>
<p>&#x5982;&#x679C;&#x4F60;&#x9009;&#x62E9;&#x5BF9;&#x7167;&#x6211;&#x4EEC;&#x7684;&#x5B9E;&#x9A8C;&#x6307;&#x5BFC;&#xFF0C;&#x81EA;&#x884C;&#x5B9E;&#x73B0;&#x4E00;&#x4E2A;&#x64CD;&#x4F5C;&#x7CFB;&#x7EDF;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x9605;&#x8BFB;&#x672C;&#x5C0F;&#x8282;&#x4F46;&#x6682;&#x65F6;&#x8DF3;&#x8FC7;&#x8FD9;&#x90E8;&#x5206;&#x7684;&#x5B9E;&#x73B0;&#x3002;&#x5B83;&#x4E0D;&#x4F1A;&#x5F71;&#x54CD;&#x4EFB;&#x4F55;&#x540E;&#x7EED;&#x5B9E;&#x9A8C;&#x7684;&#x529F;&#x80FD;&#x3002;</p>
</blockquote>
<h3 id="&#x539F;&#x7406;">&#x539F;&#x7406;</h3>
<p>&#x865A;&#x62DF;&#x5185;&#x5B58;&#x7684;&#x4E00;&#x5927;&#x4F18;&#x52BF;&#x662F;&#x53EF;&#x4EE5;&#x7528;&#x6709;&#x9650;&#x7684;&#x7269;&#x7406;&#x5185;&#x5B58;&#x7A7A;&#x95F4;&#x865A;&#x62DF;&#x51FA;&#x8FD1;&#x4E4E;&#x65E0;&#x9650;&#x7684;&#x865A;&#x62DF;&#x5185;&#x5B58;&#x7A7A;&#x95F4;&#xFF0C;&#x5176;&#x539F;&#x7406;&#x5C31;&#x662F;&#x53EA;&#x5C06;&#x4E00;&#x90E8;&#x5206;&#x865A;&#x62DF;&#x5185;&#x5B58;&#x6240;&#x5BF9;&#x5E94;&#x7684;&#x6570;&#x636E;&#x5B58;&#x653E;&#x5728;&#x7269;&#x7406;&#x5185;&#x5B58;&#x4E2D;&#xFF0C;&#x800C;&#x5269;&#x4F59;&#x7684;&#x5219;&#x5B58;&#x653E;&#x5728;&#x78C1;&#x76D8;&#xFF08;&#x5916;&#x8BBE;&#xFF09;&#x4E2D;&#x3002;&#x5F53;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x64CD;&#x4F5C;&#x5230;&#x90A3;&#x4E9B;&#x4E0D;&#x5728;&#x7269;&#x7406;&#x5185;&#x5B58;&#x4E2D;&#x7684;&#x865A;&#x62DF;&#x5730;&#x5740;&#x65F6;&#xFF0C;&#x5C31;&#x4F1A;&#x4EA7;&#x751F;<strong>&#x7F3A;&#x9875;&#x5F02;&#x5E38;&#xFF08;Page Fault&#xFF09;</strong>&#x3002;&#x6B64;&#x65F6;&#x64CD;&#x4F5C;&#x7CFB;&#x7EDF;&#x4F1A;&#x4ECB;&#x5165;&#xFF0C;&#x4EA4;&#x6362;&#x4E00;&#x90E8;&#x5206;&#x7269;&#x7406;&#x5185;&#x5B58;&#x548C;&#x78C1;&#x76D8;&#x4E2D;&#x7684;&#x6570;&#x636E;&#xFF0C;&#x4F7F;&#x5F97;&#x9700;&#x8981;&#x8BBF;&#x95EE;&#x7684;&#x5185;&#x5B58;&#x6570;&#x636E;&#x88AB;&#x653E;&#x5165;&#x7269;&#x7406;&#x5185;&#x5B58;&#x4E4B;&#x4E2D;&#x3002;</p>
<p>&#x5728;&#x9875;&#x8868;&#x4E2D;&#xFF0C;&#x9875;&#x8868;&#x9879;&#x7684; Valid &#x4F4D;&#x5C31;&#x8868;&#x793A;&#x5BF9;&#x5E94;&#x7684;&#x9875;&#x9762;&#x662F;&#x5426;&#x5728;&#x7269;&#x7406;&#x5185;&#x5B58;&#x4E2D;&#x3002;&#x56E0;&#x6B64;&#xFF0C;&#x64CD;&#x4F5C;&#x7CFB;&#x7EDF;&#x8FD8;&#x5FC5;&#x987B;&#x66F4;&#x65B0;&#x9875;&#x8868;&#xFF0C;&#x5E76;&#x5237;&#x65B0;&#x7F13;&#x5B58;&#x3002;</p>
<h3 id="&#x7F6E;&#x6362;&#x7B97;&#x6CD5;">&#x7F6E;&#x6362;&#x7B97;&#x6CD5;</h3>
<p>&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x5F88;&#x81EA;&#x7136;&#x5730;&#x8BA4;&#x4E3A;&#xFF0C;&#x64CD;&#x4F5C;&#x7CFB;&#x7EDF;&#x9700;&#x8981;&#x5C06;&#x90A3;&#x4E9B;&#x201C;&#x7ECF;&#x5E38;&#x88AB;&#x4F7F;&#x7528;&#x201D;&#x7684;&#x5185;&#x5B58;&#x7A7A;&#x95F4;&#x653E;&#x5728;&#x7269;&#x7406;&#x5185;&#x5B58;&#x4E2D;&#xFF0C;&#x800C;&#x201C;&#x4E0D;&#x90A3;&#x4E48;&#x7ECF;&#x5E38;&#x88AB;&#x4F7F;&#x7528;&#x201D;&#x7684;&#x653E;&#x5728;&#x5916;&#x90E8;&#x5B58;&#x50A8;&#x4E2D;&#x3002;&#x4F46;&#x662F;&#xFF0C;&#x8BA1;&#x7B97;&#x673A;&#x4E0D;&#x53EF;&#x80FD;&#x9884;&#x8A00;&#x54EA;&#x4E9B;&#x5730;&#x5740;&#x4F1A;&#x5728;&#x4EE5;&#x540E;&#x88AB;&#x8BBF;&#x95EE;&#x3002;&#x6211;&#x4EEC;&#x53EA;&#x80FD;&#x901A;&#x8FC7;&#x4E00;&#x4E9B;&#x7F6E;&#x6362;&#x7B97;&#x6CD5;&#xFF0C;&#x6839;&#x636E;&#x524D;&#x4E00;&#x6BB5;&#x65F6;&#x95F4;&#x7684;&#x5185;&#x5B58;&#x4F7F;&#x7528;&#x60C5;&#x51B5;&#xFF0C;&#x6765;&#x4F30;&#x8BA1;&#x672A;&#x6765;&#x54EA;&#x4E9B;&#x5730;&#x5740;&#x4F1A;&#x88AB;&#x4F7F;&#x7528;&#xFF0C;&#x4ECE;&#x800C;&#x5C06;&#x8FD9;&#x90E8;&#x5206;&#x6570;&#x636E;&#x4FDD;&#x7559;&#x5728;&#x7269;&#x7406;&#x5185;&#x5B58;&#x4E2D;&#x3002;</p>
<p>&#x6734;&#x7D20;&#x5730;&#x8003;&#x8651;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x5047;&#x8BBE;&#x5982;&#x679C;&#x7A0B;&#x5E8F;&#x521A;&#x521A;&#x8BBF;&#x95EE;&#x4E86;&#x4E00;&#x90E8;&#x5206;&#x5185;&#x5B58;&#xFF0C;&#x90A3;&#x4E48;&#x4ED6;&#x5C31;&#x5F88;&#x6709;&#x53EF;&#x80FD;&#x91CD;&#x590D;&#x5730;&#x8BBF;&#x95EE;&#x5B83;&#x3002;&#x56E0;&#x6B64;&#xFF0C;&#x6BCF;&#x6B21;&#x51FA;&#x73B0;&#x7F3A;&#x9875;&#x65F6;&#xFF0C;&#x5C31;&#x5C06;&#x7269;&#x7406;&#x5185;&#x5B58;&#x4E2D;&#x6700;&#x540E;&#x8BBF;&#x95EE;&#x65F6;&#x95F4;&#x6700;&#x9760;&#x524D;&#x7684;&#x9875;&#x9762;&#x66FF;&#x6362;&#x51FA;&#x53BB;&#x3002;&#x8FD9;&#x5C31;&#x662F;<strong>LRU (Least Recently Used) &#x7B97;&#x6CD5;</strong>&#x3002;&#x4F46;&#x8FD9;&#x79CD;&#x7B97;&#x6CD5;&#x9700;&#x8981;&#x7EF4;&#x62A4;&#x4E00;&#x4E2A;&#x4F18;&#x5148;&#x961F;&#x5217;&#xFF0C;&#x800C;&#x4E14;&#x5728;&#x6BCF;&#x4E00;&#x6B21;&#x8BBF;&#x95EE;&#x5185;&#x5B58;&#x65F6;&#x90FD;&#x8981;&#x66F4;&#x65B0;&#x3002;&#x5F88;&#x663E;&#x7136;&#x8FD9;&#x662F;&#x4E0D;&#x73B0;&#x5B9E;&#x7684;&#xFF0C;&#x5B83;&#x5E26;&#x6765;&#x7684;&#x5F00;&#x9500;&#x592A;&#x5927;&#x3002;</p>
<p>&#x76EE;&#x524D;&#x5B58;&#x5728;&#x7740;&#x5927;&#x91CF;&#x7684;&#x7F6E;&#x6362;&#x7B97;&#x6CD5;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x5728;<a href="https://en.wikipedia.org/wiki/Page_replacement_algorithm" target="_blank">&#x7EF4;&#x57FA;&#x767E;&#x79D1;</a>&#x4E0A;&#x521D;&#x6B65;&#x4E86;&#x89E3;&#x3002;</p>
<h3 id="&#x6211;&#x4EEC;&#x7684;&#x5B9E;&#x73B0;">&#x6211;&#x4EEC;&#x7684;&#x5B9E;&#x73B0;</h3>
<p>&#x9996;&#x5148;&#xFF0C;&#x6211;&#x4EEC;&#x8981;&#x5728;&#x78C1;&#x76D8;&#x4E2D;&#x5EFA;&#x7ACB;&#x4E00;&#x4E2A;&#x9875;&#x9762;&#x7F6E;&#x6362;&#x6587;&#x4EF6;&#xFF0C;&#x6765;&#x4FDD;&#x5B58;&#x6240;&#x6709;&#x6362;&#x51FA;&#x7684;&#x9875;&#x9762;&#x3002;&#x4E3A;&#x4E86;&#x7B80;&#x5316;&#x5B9E;&#x73B0;&#xFF0C;&#x6211;&#x4EEC;&#x76F4;&#x63A5;&#x5728;&#x955C;&#x50CF;&#x4E2D;&#x6253;&#x5305;&#x4E00;&#x4E2A;&#x5168;&#x662F; 0 &#x7684;&#x6587;&#x4EF6; <code>SWAP_FILE</code> &#x8FDB;&#x53BB;&#x3002;</p>
<p></p><p class="code-label">user/Makefile</p><p></p>
<pre><code class="lang-makefile"><span class="hljs-comment"># &#x7F16;&#x8BD1;&#x3001;&#x6253;&#x5305;&#x3001;&#x683C;&#x5F0F;&#x8F6C;&#x6362;&#x3001;&#x9884;&#x7559;&#x7A7A;&#x95F4;</span>
build: dependency
    @cargo build
    @echo Targets: $(patsubst $(SRC_DIR)/%.rs, %, $(SRC_FILES))
    @rm -rf $(OUT_DIR)
    @mkdir -p $(OUT_DIR)
    @cp $(BIN_FILES) $(OUT_DIR)
--&gt;    @dd if=/dev/zero of=$(OUT_DIR)/SWAP_FILE bs=1M count=16
    @rcore-fs-fuse --fs sfs $(IMG_FILE) $(OUT_DIR) zip
    @qemu-img convert -f raw $(IMG_FILE) -O qcow2 $(QCOW_FILE)
    @qemu-img resize $(QCOW_FILE) +1G
</code></pre>
<p>&#x6211;&#x4EEC;&#x5E0C;&#x671B;&#x6BCF;&#x4E2A;&#x8FDB;&#x7A0B;&#x7684; <code>Mapping</code> &#x90FD;&#x80FD;&#x591F;&#x50CF;&#x7BA1;&#x7406;&#x7269;&#x7406;&#x9875;&#x9762;&#x4E00;&#x6837;&#x7BA1;&#x7406;&#x8FD9;&#x4E9B;&#x7F6E;&#x6362;&#x9875;&#x9762;&#xFF08;&#x5728;&#x9500;&#x6BC1;&#x65F6;&#x4E5F;&#x80FD;&#x591F;&#x91CA;&#x653E;&#x5B83;&#x4EEC;&#xFF09;&#xFF0C;&#x56E0;&#x6B64;&#x6211;&#x4EEC;&#x5B9E;&#x73B0;&#x4E86;&#x4E00;&#x4E2A;&#x7C7B;&#x4F3C;&#x4E8E; <code>FrameTracker</code> &#x7684; <code>SwapTracker</code>&#x3002;&#x5B83;&#x7684;&#x5177;&#x4F53;&#x5B9E;&#x73B0;&#x4F1A;&#x7528;&#x5230;&#x4E00;&#x4E9B;&#x6587;&#x4EF6;&#x7CFB;&#x7EDF;&#x7684;&#x64CD;&#x4F5C;&#xFF0C;&#x5982;&#x679C;&#x611F;&#x5174;&#x8DA3;&#x53EF;&#x4EE5;&#x53C2;&#x8003;&#x6E90;&#x7801;&#x3002;&#x4F46;&#x7B80;&#x6982;&#x62EC;&#x4E4B;&#xFF1A;<strong><code>SwapTracker</code> &#x8BB0;&#x5F55;&#x4E86;&#x4E00;&#x4E2A;&#x88AB;&#x7F6E;&#x6362;&#x51FA;&#x7269;&#x7406;&#x5185;&#x5B58;&#x7684;&#x9875;&#x9762;&#xFF0C;&#x5E76;&#x63D0;&#x4F9B;&#x4E00;&#x4E9B;&#x4FBF;&#x6377;&#x7684;&#x64CD;&#x4F5C;&#x63A5;&#x53E3;</strong>&#x3002;</p>
<p></p><p class="code-label">os/src/fs/swap.rs</p><p></p>
<pre><code class="lang-rust"><span class="hljs-comment">/// &#x7C7B;&#x4F3C;&#x4E8E; [`FrameTracker`]&#xFF0C;&#x76F8;&#x5F53;&#x4E8E; `Box&lt;&#x7F6E;&#x6362;&#x6587;&#x4EF6;&#x4E2D;&#x7684;&#x4E00;&#x4E2A;&#x9875;&#x9762;&gt;`</span>
<span class="hljs-comment">///</span>
<span class="hljs-comment">/// &#x5185;&#x90E8;&#x4FDD;&#x5B58;&#x8BE5;&#x7F6E;&#x6362;&#x9875;&#x9762;&#x5728;&#x6587;&#x4EF6;&#x4E2D;&#x4FDD;&#x5B58;&#x7684; index</span>
<span class="hljs-comment">///</span>
<span class="hljs-comment">/// [`FrameTracker`]: crate::memory::frame::FrameTracker</span>
<span class="hljs-meta">#[derive(Debug, Ord, PartialOrd, Eq, PartialEq, Hash)]</span>
<span class="hljs-keyword">pub</span> <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">SwapTracker</span></span>(<span class="hljs-keyword">pub</span>(<span class="hljs-keyword">super</span>) usize);

<span class="hljs-keyword">impl</span> SwapTracker {
    <span class="hljs-comment">/// &#x4ECE;&#x7F6E;&#x6362;&#x6587;&#x4EF6;&#x5206;&#x914D;&#x4E00;&#x4E2A;&#x9875;&#x9762;&#x7A7A;&#x95F4;</span>
    <span class="hljs-keyword">pub</span> <span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">new</span></span>() -&gt; MemoryResult&lt;<span class="hljs-keyword">Self</span>&gt; {
        ...
    }

    <span class="hljs-comment">/// &#x8BFB;&#x53D6;&#x9875;&#x9762;&#x6570;&#x636E;</span>
    <span class="hljs-keyword">pub</span> <span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">read</span></span>(&amp;<span class="hljs-keyword">self</span>) -&gt; [<span class="hljs-keyword">u8</span>; PAGE_SIZE] {
        ...
    }

    <span class="hljs-comment">/// &#x5199;&#x5165;&#x9875;&#x9762;&#x6570;&#x636E;</span>
    <span class="hljs-keyword">pub</span> <span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">write</span></span>(&amp;<span class="hljs-keyword">self</span>, data: &amp;[<span class="hljs-keyword">u8</span>; PAGE_SIZE]) {
        ...
    }
}

<span class="hljs-keyword">impl</span> <span class="hljs-built_in">Drop</span> <span class="hljs-keyword">for</span> SwapTracker {
    <span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">drop</span></span>(&amp;<span class="hljs-keyword">mut</span> <span class="hljs-keyword">self</span>) {
        ...
    }
}
</code></pre>
<p>&#x7136;&#x540E;&#xFF0C;&#x6211;&#x4EEC;&#x5B9A;&#x4E49;&#x4E86;&#x4E00;&#x4E2A;&#x7F6E;&#x6362;&#x7B97;&#x6CD5;&#x7684;&#x63A5;&#x53E3;&#xFF0C;&#x5E76;&#x4E14;&#x5B9E;&#x73B0;&#x4E86;&#x4E00;&#x4E2A;&#x975E;&#x5E38;&#x7B80;&#x5355;&#x7684;&#x7F6E;&#x6362;&#x7B97;&#x6CD5;&#xFF0C;&#x5177;&#x4F53;&#x7B97;&#x6CD5;&#x5C31;&#x4E0D;&#x5448;&#x73B0;&#x4E86;&#x3002;</p>
<p></p><p class="code-label">os/src/memory/mapping/swapper.rs</p><p></p>
<pre><code class="lang-rust"><span class="hljs-comment">/// &#x7BA1;&#x7406;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x6240;&#x6620;&#x5C04;&#x7684;&#x9875;&#x9762;&#x7684;&#x7F6E;&#x6362;&#x64CD;&#x4F5C;</span>
<span class="hljs-keyword">pub</span> <span class="hljs-class"><span class="hljs-keyword">trait</span> <span class="hljs-title">Swapper</span></span> {
    <span class="hljs-comment">/// &#x65B0;&#x5EFA;&#x5E26;&#x6709;&#x4E00;&#x4E2A;&#x5206;&#x914D;&#x6570;&#x91CF;&#x4E0A;&#x9650;&#x7684;&#x7F6E;&#x6362;&#x5668;</span>
    <span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">new</span></span>(quota: usize) -&gt; <span class="hljs-keyword">Self</span>;

    <span class="hljs-comment">/// &#x662F;&#x5426;&#x5DF2;&#x8FBE;&#x5230;&#x4E0A;&#x9650;</span>
    <span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">full</span></span>(&amp;<span class="hljs-keyword">self</span>) -&gt; <span class="hljs-keyword">bool</span>;

    <span class="hljs-comment">/// &#x53D6;&#x51FA;&#x4E00;&#x7EC4;&#x6620;&#x5C04;</span>
    <span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">pop</span></span>(&amp;<span class="hljs-keyword">mut</span> <span class="hljs-keyword">self</span>) -&gt; <span class="hljs-built_in">Option</span>&lt;(VirtualPageNumber, FrameTracker)&gt;;

    <span class="hljs-comment">/// &#x6DFB;&#x52A0;&#x4E00;&#x7EC4;&#x6620;&#x5C04;&#xFF08;&#x4E0D;&#x4F1A;&#x5728;&#x4EE5;&#x8FBE;&#x5230;&#x5206;&#x914D;&#x4E0A;&#x9650;&#x65F6;&#x8C03;&#x7528;&#xFF09;</span>
    <span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">push</span></span>(&amp;<span class="hljs-keyword">mut</span> <span class="hljs-keyword">self</span>, vpn: VirtualPageNumber, frame: FrameTracker);

    <span class="hljs-comment">/// &#x53EA;&#x4FDD;&#x7559;&#x7B26;&#x5408;&#x67D0;&#x79CD;&#x6761;&#x4EF6;&#x7684;&#x6761;&#x76EE;&#xFF08;&#x7528;&#x4E8E;&#x79FB;&#x9664;&#x4E00;&#x6BB5;&#x865A;&#x62DF;&#x5730;&#x5740;&#xFF09;</span>
    <span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">retain</span></span>(&amp;<span class="hljs-keyword">mut</span> <span class="hljs-keyword">self</span>, predicate: <span class="hljs-keyword">impl</span> <span class="hljs-built_in">Fn</span>(&amp;VirtualPageNumber) -&gt; <span class="hljs-keyword">bool</span>);
}
</code></pre>
<p>&#x8FD9;&#x91CC;&#xFF0C;<code>Swapper</code> &#x5C31;&#x66FF;&#x4EE3;&#x4E86; <code>Mapping</code> &#x4E2D;&#x7684; <code>mapped_pairs: Vec&lt;(VirtualPageNumber, FrameTracker)&gt;</code> &#x7684;&#x4F5C;&#x7528;&#x3002;&#x56E0;&#x6B64;&#xFF0C;&#x6211;&#x4EEC;&#x66FF;&#x6362; <code>Mapping</code> &#x4E2D;&#x7684;&#x6210;&#x5458;&#xFF1A;</p>
<p></p><p class="code-label">os/src/memory/mapping/mapping.rs</p><p></p>
<pre><code class="lang-rust"><span class="hljs-comment">/// &#x67D0;&#x4E2A;&#x8FDB;&#x7A0B;&#x7684;&#x5185;&#x5B58;&#x6620;&#x5C04;&#x5173;&#x7CFB;</span>
<span class="hljs-keyword">pub</span> <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">Mapping</span></span> {
    <span class="hljs-comment">/// &#x4FDD;&#x5B58;&#x6240;&#x6709;&#x4F7F;&#x7528;&#x5230;&#x7684;&#x9875;&#x8868;</span>
    page_tables: <span class="hljs-built_in">Vec</span>&lt;PageTableTracker&gt;,
    <span class="hljs-comment">/// &#x6839;&#x9875;&#x8868;&#x7684;&#x7269;&#x7406;&#x9875;&#x53F7;</span>
    root_ppn: PhysicalPageNumber,
    <span class="hljs-comment">/// &#x6240;&#x6709;&#x5206;&#x914D;&#x7684;&#x7269;&#x7406;&#x9875;&#x9762;&#x6620;&#x5C04;&#x4FE1;&#x606F;</span>
    mapped_pairs: SwapperImpl,
    <span class="hljs-comment">/// &#x88AB;&#x6362;&#x51FA;&#x7684;&#x9875;&#x9762;&#x5B58;&#x50A8;&#x5728;&#x865A;&#x62DF;&#x5185;&#x5B58;&#x6587;&#x4EF6;&#x4E2D;&#x7684; Tracker</span>
    swapped_pages: HashMap&lt;VirtualPageNumber, SwapTracker&gt;,
}
</code></pre>
<p>&#x6700;&#x540E;&#xFF0C;&#x8BA9;&#x6211;&#x4EEC;&#x5B9E;&#x73B0;&#x5185;&#x5B58;&#x7F6E;&#x6362;&#xFF1A;&#x9047;&#x5230;&#x7F3A;&#x9875;&#x5F02;&#x5E38;&#xFF0C;&#x627E;&#x5230;&#x9700;&#x8981;&#x8BBF;&#x95EE;&#x7684;&#x9875;&#x53F7;&#x3001;&#x9700;&#x8981;&#x8BBF;&#x95EE;&#x7684;&#x9875;&#x9762;&#x6570;&#x636E;&#xFF0C;&#x5E76;&#x7F6E;&#x6362;&#x51FA;&#x4E00;&#x4E2A;&#x7269;&#x7406;&#x5185;&#x5B58;&#x4E2D;&#x7684;&#x9875;&#x53F7;&#x3001;&#x9875;&#x9762;&#x6570;&#x636E;&#xFF0C;&#x5C06;&#x4E8C;&#x8005;&#x8FDB;&#x884C;&#x4EA4;&#x6362;</p>
<p></p><p class="code-label">os/src/memory/mapping/mapping.rs</p><p></p>
<pre><code class="lang-rust"><span class="hljs-keyword">impl</span> Mapping {
    <span class="hljs-comment">/// &#x5904;&#x7406;&#x7F3A;&#x9875;&#x5F02;&#x5E38;</span>
    <span class="hljs-keyword">pub</span> <span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">handle_page_fault</span></span>(&amp;<span class="hljs-keyword">mut</span> <span class="hljs-keyword">self</span>, stval: usize) -&gt; MemoryResult&lt;()&gt; {
        <span class="hljs-keyword">let</span> vpn = VirtualPageNumber::floor(stval.into());
        <span class="hljs-keyword">let</span> swap_tracker = <span class="hljs-keyword">self</span>
            .swapped_pages
            .remove(&amp;vpn)
            .ok_or(<span class="hljs-string">&quot;stval page is not mapped&quot;</span>)?;
        <span class="hljs-keyword">let</span> page_data = swap_tracker.read();

        <span class="hljs-keyword">if</span> <span class="hljs-keyword">self</span>.mapped_pairs.full() {
            <span class="hljs-comment">// &#x53D6;&#x51FA;&#x4E00;&#x4E2A;&#x6620;&#x5C04;</span>
            <span class="hljs-keyword">let</span> (popped_vpn, <span class="hljs-keyword">mut</span> popped_frame) = <span class="hljs-keyword">self</span>.mapped_pairs.pop().unwrap();
            <span class="hljs-comment">// print!(&quot;{:x?} -&gt; {:x?}&quot;, popped_vpn, vpn);</span>
            <span class="hljs-comment">// &#x4EA4;&#x6362;&#x6570;&#x636E;</span>
            swap_tracker.write(&amp;*popped_frame);
            (*popped_frame).copy_from_slice(&amp;page_data);
            <span class="hljs-comment">// &#x4FEE;&#x6539;&#x9875;&#x8868;&#x6620;&#x5C04;</span>
            <span class="hljs-keyword">self</span>.invalidate_one(popped_vpn)?;
            <span class="hljs-keyword">self</span>.remap_one(vpn, popped_frame.page_number())?;
            <span class="hljs-comment">// &#x66F4;&#x65B0;&#x8BB0;&#x5F55;</span>
            <span class="hljs-keyword">self</span>.mapped_pairs.push(vpn, popped_frame);
            <span class="hljs-keyword">self</span>.swapped_pages.insert(popped_vpn, swap_tracker);
        } <span class="hljs-keyword">else</span> {
            <span class="hljs-comment">// &#x5982;&#x679C;&#x5F53;&#x524D;&#x8FD8;&#x6CA1;&#x6709;&#x8FBE;&#x5230;&#x914D;&#x989D;&#xFF0C;&#x5219;&#x53EF;&#x4EE5;&#x7EE7;&#x7EED;&#x5206;&#x914D;&#x7269;&#x7406;&#x9875;&#x9762;&#x3002;&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#x76EE;&#x524D;&#x8FD8;&#x4E0D;&#x4F1A;&#x51FA;&#x73B0;</span>
            <span class="hljs-comment">// &#x6DFB;&#x52A0;&#x65B0;&#x7684;&#x6620;&#x5C04;</span>
            <span class="hljs-keyword">let</span> <span class="hljs-keyword">mut</span> frame = FRAME_ALLOCATOR.lock().alloc()?;
            <span class="hljs-comment">// &#x590D;&#x5236;&#x6570;&#x636E;</span>
            (*frame).copy_from_slice(&amp;page_data);
            <span class="hljs-comment">// &#x66F4;&#x65B0;&#x6620;&#x5C04;</span>
            <span class="hljs-keyword">self</span>.remap_one(vpn, frame.page_number())?;
            <span class="hljs-comment">// &#x66F4;&#x65B0;&#x8BB0;&#x5F55;</span>
            <span class="hljs-keyword">self</span>.mapped_pairs.push(vpn, frame);
        }
        <span class="hljs-literal">Ok</span>(())
    }
}
</code></pre>
<p>&#x7136;&#x540E;&#xFF0C;&#x4EE4;&#x7F3A;&#x9875;&#x5F02;&#x5E38;&#x8C03;&#x7528;&#x4E0A;&#x9762;&#x7684;&#x51FD;&#x6570;&#xFF0C;&#x5C31;&#x5B8C;&#x6210;&#x4E86;&#x9875;&#x9762;&#x7F6E;&#x6362;&#x7684;&#x5B9E;&#x73B0;</p>
<p></p><p class="code-label">os/src/interrupt/handler.rs</p><p></p>
<pre><code class="lang-rust"><span class="hljs-comment">/// &#x5904;&#x7406;&#x7F3A;&#x9875;&#x5F02;&#x5E38;</span>
<span class="hljs-comment">///</span>
<span class="hljs-comment">/// todo: &#x7406;&#x8BBA;&#x4E0A;&#x8FD9;&#x91CC;&#x9700;&#x8981;&#x5224;&#x65AD;&#x8BBF;&#x95EE;&#x7C7B;&#x578B;&#xFF0C;&#x5E76;&#x4E0E;&#x9875;&#x8868;&#x4E2D;&#x7684;&#x6807;&#x5FD7;&#x4F4D;&#x8FDB;&#x884C;&#x6BD4;&#x5BF9;</span>
<span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">page_fault</span></span>(context: &amp;<span class="hljs-keyword">mut</span> Context, stval: usize) -&gt; <span class="hljs-built_in">Result</span>&lt;*<span class="hljs-keyword">mut</span> Context, <span class="hljs-built_in">String</span>&gt; {
    <span class="hljs-built_in">println!</span>(<span class="hljs-string">&quot;page_fault&quot;</span>);
    <span class="hljs-keyword">let</span> current_thread = PROCESSOR.lock().current_thread();
    <span class="hljs-keyword">let</span> memory_set = &amp;<span class="hljs-keyword">mut</span> current_thread.process.write().memory_set;
    memory_set.mapping.handle_page_fault(stval)?;
    memory_set.activate();
    <span class="hljs-literal">Ok</span>(context)
}
</code></pre>
<p><div id="page-footer" class="localized-footer"><hr><p><link rel="stylesheet" href="https://unpkg.com/gitalk/dist/gitalk.css"></p>
<script src="https://unpkg.com/gitalk/dist/gitalk.min.js"></script>
<div id="gitalk-container"></div>
<script>
    var gitalk = new Gitalk({
        clientID: "5084c46751a4b2f7cd4d",
        clientSecret: "3904f62d33aef24f19480fedc836a48b31d532e9",
        repo: "rCore-Tutorial-deploy",
        owner: "rcore-os",
        admin: [
            "chyyuu",
            "wangrunji0408",
            "xy-plus",
            "wyfcyx",
            "LyricZhao",
            "Tuyixiang"
        ],
        id: location.pathname.replace('/rCore-Tutorial-deploy', '').replace('.html', ''),
        distractionFreeMode: false
    });
    gitalk.render("gitalk-container");
</script></div></p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="part-4.html" class="navigation navigation-prev " aria-label="Previous page: 实现内核重映射">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="summary.html" class="navigation navigation-next " aria-label="Next page: 小结">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"页面置换*","level":"3.4.6","depth":2,"next":{"title":"小结","level":"3.4.7","depth":2,"path":"docs/lab-3/guide/summary.md","ref":"docs/lab-3/guide/summary.md","articles":[]},"previous":{"title":"实现内核重映射","level":"3.4.5","depth":2,"path":"docs/lab-3/guide/part-4.md","ref":"docs/lab-3/guide/part-4.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["hide-element","chapter-fold","katex","alerts","emphasize","mermaid-gb3","codeblock-label","code","search-pro","click-reveal","expandable-chapters-interactive","localized-footer"],"pluginsConfig":{"chapter-fold":{},"emphasize":{},"codeblock-label":{},"search-pro":{},"search":{},"localized-footer":{"filename":"gitalk.html","hline":"true"},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"code":{"copyButtons":true},"hide-element":{},"katex":{},"fontsettings":{"theme":"white","family":"sans","size":1},"click-reveal":{},"highlight":{},"mermaid-gb3":{},"expandable-chapters-interactive":{},"alerts":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"docs/lab-3/guide/part-5.md","mtime":"2020-10-01T11:39:40.511Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-10-01T11:40:38.595Z"},"basePath":"../../..","book":{"language":""}});
        });
    </script>
</div>

        
    
    <script src="../../../gitbook/gitbook.js"></script>
    <script src="../../../gitbook/theme.js"></script>
    
        
        <script src="../../../gitbook/gitbook-plugin-hide-element/plugin.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-chapter-fold/chapter-fold.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-alerts/plugin.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-mermaid-gb3/book/plugin.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-code/plugin.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-search-pro/jquery.mark.min.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-search-pro/search.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-click-reveal/click_reveal.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-expandable-chapters-interactive/expandable-chapters.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    <script src="../../../gitbook/gitbook-plugin-mermaid-gb3/mermaid/mermaid.min.js"></script>

    </body>
</html>

